<template>
  <div class="shift-setting">
    <h3>班次设置</h3>
    <el-form :model="shiftForm" label-width="120px" class="setting-form">
      <!-- 班次列表 -->
      <el-card class="setting-card">
        <div slot="header">
          <span>班次列表</span>
          <el-button style="float: right" type="primary" size="small" @click="handleAddShift">添加班次</el-button>
        </div>
        <el-table :data="shiftForm.shifts" border style="width: 100%">
          <el-table-column prop="name" label="班次名称" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.name" placeholder="请输入班次名称" />
            </template>
          </el-table-column>
          <el-table-column prop="startTime" label="上班时间" width="180">
            <template slot-scope="scope">
              <el-time-picker v-model="scope.row.startTime" format="HH:mm" placeholder="请选择上班时间" />
            </template>
          </el-table-column>
          <el-table-column prop="endTime" label="下班时间" width="180">
            <template slot-scope="scope">
              <el-time-picker v-model="scope.row.endTime" format="HH:mm" placeholder="请选择下班时间" />
            </template>
          </el-table-column>
          <el-table-column prop="restStartTime" label="休息开始时间" width="180">
            <template slot-scope="scope">
              <el-time-picker v-model="scope.row.restStartTime" format="HH:mm" placeholder="请选择休息开始时间" />
            </template>
          </el-table-column>
          <el-table-column prop="restEndTime" label="休息结束时间" width="180">
            <template slot-scope="scope">
              <el-time-picker v-model="scope.row.restEndTime" format="HH:mm" placeholder="请选择休息结束时间" />
            </template>
          </el-table-column>
          <el-table-column label="操作" width="120">
            <template slot-scope="scope">
              <el-button type="danger" size="mini" @click="handleDeleteShift(scope.$index)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>

      <!-- 保存按钮 -->
      <el-form-item>
        <el-button type="primary" @click="handleSave">保存设置</el-button>
        <el-button @click="handleReset">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'ShiftSetting',
  data() {
    return {
      shiftForm: {
        shifts: [
          {
            name: '早班',
            startTime: new Date(2000, 1, 1, 8, 0),
            endTime: new Date(2000, 1, 1, 16, 0),
            restStartTime: new Date(2000, 1, 1, 12, 0),
            restEndTime: new Date(2000, 1, 1, 13, 0)
          },
          {
            name: '中班',
            startTime: new Date(2000, 1, 1, 16, 0),
            endTime: new Date(2000, 1, 1, 24, 0),
            restStartTime: new Date(2000, 1, 1, 20, 0),
            restEndTime: new Date(2000, 1, 1, 21, 0)
          }
        ]
      }
    }
  },
  methods: {
    handleAddShift() {
      this.shiftForm.shifts.push({
        name: '',
        startTime: null,
        endTime: null,
        restStartTime: null,
        restEndTime: null
      })
    },
    handleDeleteShift(index) {
      this.shiftForm.shifts.splice(index, 1)
    },
    handleSave() {
      // TODO: 调用API保存设置
      this.$message.success('班次设置保存成功')
    },
    handleReset() {
      this.$confirm('确定要重置班次设置吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // TODO: 重置为默认设置
        this.$message.success('班次设置已重置')
      }).catch(() => {})
    }
  }
}
</script>

<style scoped>
.shift-setting {
  padding: 20px;
}

.setting-form {
  max-width: 1200px;
}

.setting-card {
  margin-bottom: 20px;
}

.el-card ::v-deep .el-card__header {
  padding: 10px 20px;
  font-weight: bold;
}
</style>